package Action;

import Algorithm.Recommended;
import Entities.DoeBookEntity;
import Service.RecommendService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//控制推荐的Action（其实没用到，获取推荐列表的功能在HomeAction中）
public class RecommendAction implements Action
{
    private RecommendService recommendService;
    private List<DoeBookEntity> recommendList;
    private int initLimit = 14;
    private String after_id = "0";
    private String limit=String.valueOf(initLimit);
    private List<DoeBookEntity> feedList;

    //which is important to the converting to json using struts
    public List<DoeBookEntity> getFeedList() {
        return feedList;
    }

    public void setFeedList(List<DoeBookEntity> feedList) {
        this.feedList = feedList;
    }

    public String getAfter_id() {
        return after_id;
    }

    public void setAfter_id(String after_id) {
        this.after_id = after_id;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

    public List<DoeBookEntity> getRecommendList() {
        return recommendList;
    }

    public void setRecommendList(List<DoeBookEntity> recommendList) {
        this.recommendList = recommendList;
    }

    public RecommendService getRecommendService() {
        return recommendService;
    }

    public void setRecommendService(RecommendService recommendService) {
        this.recommendService = recommendService;
    }


    public String execute()
    {
        String param_after_id = ServletActionContext.getRequest().getParameter("after_id");
        String param_limit = ServletActionContext.getRequest().getParameter("limit");
        setAfter_id(param_after_id != null ? param_after_id : "0");
        setLimit(param_limit != null ? param_limit : String.valueOf(initLimit));
        Map<String, Object> session =  ActionContext.getContext().getSession();
        String user_name = (String) session.get("user_name");  //获取登录或注册时存入session的参数——用户名

        String session_Id = ServletActionContext.getRequest().getSession().getId();

        if (Integer.parseInt(after_id) >= initLimit)
        {
            setFeedList(recommendService.recommend(user_name, Integer.parseInt(after_id),Integer.parseInt(limit), session_Id));
            return "append";
        }
        else
        {
            setRecommendList(recommendService.recommend(user_name, Integer.parseInt(after_id),Integer.parseInt(limit), session_Id));
            return "success";
        }
    }



}
